import datetime
import requests

fp = open('weblog.txt', 'r')
fp.seek(0)
ips = []
iptime = []
dk = []
for line in fp:
    line = line.split(' ')
    ips.append(line[0])
    iptime.append(line[3].removeprefix('['))
    dk.append(line[9])
fp.close()
iptime2 = []

for Time in iptime:
    iptime2.append(datetime.datetime.strptime(Time, '%d/%b/%Y:%H:%M:%S').strftime('%Y-%m-%d %H:%M:%S'))

fp2 = open('result.txt', 'w', encoding='utf-8')
total = len(ips)

for result in range(total):
    information = requests.get(f'https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip={ips[result]}').json()
    Region = information['data']['region']
    ISP = information['data']['isp']
    fp2.write(f'{iptime2[result]} {Region} {ISP} {dk[result]}\n')
